<?php echo Tag::css('jquery/ui/css/ui-lightness/jquery-ui-1.8.18.custom'); ?>
<?php echo Tag::css('jqGrid-4.1.1/css/ui.jqgrid'); ?>
<?php echo Tag::css('jqGrid-4.1.1/css/ui.multiselect'); ?>
<?php echo Tag::js('jqGrid-4.1.1/js/i18n/grid.locale-es'); ?>
<?php echo Tag::js('jqGrid-4.1.1/js/jquery.jqGrid.min'); ?>

<?php echo Tag::js('jquery/ui/js/jquery-ui-1.8.18.custom.min'); ?>
<?php echo Tag::js('jqGrid/plugins/jquery.layout'); ?>

<?php echo Tag::js('jqGrid-4.1.1/plugins/jquery.tablednd'); ?>
<?php echo Tag::js('jqGrid-4.1.1/plugins/jquery.contextmenu'); ?>
<?php echo Tag::js('jqGrid-4.1.1/plugins/ui.multiselect'); ?>

<?php

     $model = ''; 
     $names = '';
     foreach ($dataGrid->fields as $campo):
          $names .= '"'.$campo.'",' ;

$formato = $dataGrid->getformatOption($campo);

          $key = $dataGrid->getkey($campo);
          $model .= '{name:"'.$campo.'",index:"'.$campo.'", width:120, xmlmap:"ItemAttributes>'.$campo.'", sorttype:"integer", sorttype:"int"'.$key . $formato.'},';
     endforeach; ?>

<?php $datos ="'".'<?xml version="1.0" encoding="UTF-8" ?>' ; $datos .= $dataGrid->xml() ."'"; ?> <script type="text/javascript"> var datos = <?php echo "$datos"; ?>


jQuery(document).ready(function(){

   var lastsel;
   jQuery("#list").jqGrid({ 
     url: "Listado", 
     datastr:datos,
     datatype: "xmlstring", 
     colNames:[<?php  echo substr($names, 0,-1);?>], 
     colModel:[<?php  echo substr($model, 0,-1);?>],
     height:250, with: 450,
     loadonce: true, 
     xmlReader: { root : "Items", row: "Item", repeatitems: false, id: "ASIN" }, 
     rowNum:5, 
     rowList:[5,10,20,30], 
     pager:jQuery('#pagered'), //'#pagered', 
     sortname: 'id', 
     viewrecords: true, 
     sortorder: "desc", 
     caption: "Listado",     
     cellEdit: false, 
     cellsubmit: 'clientArray',     

onSelectRow: function(rowid){

              lastsel = $('#list').getCell(rowid, 'id');

}, ondblClickRow: function(rowid){

              lastsel = $('#list').getCell(rowid, 'id');

}, footerrow : true, userDataOnFooter : true, altRows : true,

     //     grouping:true, 
     //     groupingView : { 
     //     groupField : ['debe'],
     //     groupDataSorted : true           
     // }, 
      // caption: "Grouping",
     //  groupSummary: true,
      // groupDataSorted: true
   });


	jQuery("#list").jqGrid('navGrid','#pagered', {edit:false,add:false,del:false}, {}, {}, {}, {multipleSearch:true, multipleGroup:true, showQuery: false} );
	
	jQuery("#list").jqGrid('navButtonAdd',"#pagered",{
	
	  caption:"",
	  buttonicon:'ui-icon-plus', 
	  onClickButton: function(){ 
	        location.href='/<?php echo $dataGrid->url ?>create/';
	  }, 
	  position:"last"
	
	}); jQuery("#list").jqGrid('navButtonAdd',"#pagered",{
	
	  caption:"",
	  buttonicon:"ui-icon-pencil", 
	  onClickButton: function(){ 
	
	// var gsr = jQuery("#list").jqGrid('getGridParam','selrow'); // if(gsr){ // jQuery("#list").jqGrid('GridToForm',gsr,"#order"); // } else { // alert("Seleccione un registro") // }
	
	    if(lastsel){
	               location.href="/<?php echo $dataGrid->url ?>edit/"+lastsel;
	         }else{
	               alert("Seleccione un registro")
	    }
	    
	  }, 
	  position:"last"
	
	});
	
	jQuery("#list").jqGrid('navButtonAdd',"#pagered",{
	
	  caption:"",
	  buttonicon:"ui-icon-trash", 
	  onClickButton: function(){ 
	
	/* var gsr = jQuery("#list").jqGrid('getGridParam','selrow'); if(gsr){ jQuery("#list").jqGrid('GridToForm',gsr,"#order"); } else { alert("Seleccione un registro") }*/
	
	    if(lastsel){
	                location.href="/<?php echo $dataGrid->url ?>del/"+lastsel;
	         }else{
	               alert("Seleccione un registro")
	    }
	     
	  }, 
	  position:"last"
	
	});

});

function mysum(val, name, record){

  return parseFloat(val||0) + parseFloat((record[name]||0));

}

function currencyFmatter (cellvalue, options, rowObject){

  return "$"+cellvalue;

};

   // $("#list").jqGrid('bindKeys', {"onEnter":function( rowid ) { alert("You enter a row with id:"+rowid)} } );

function formatOperations(cellvalue, options, rowObject) {

     return "<a href='/<?php echo $dataGrid->url ?>edit/" + cellvalue + "' id='ui-icon-trash'>Edit</a>  "+
 	       "<a <a href='/<?php echo $dataGrid->headers ?>del/" + cellvalue + "'>Edit</a>";

};



</script> 